package com.hundsun.yr.universal.p2p.interactor.impl;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.hundsun.mobile.zwgt.R;
import com.hundsun.yr.universal.p2p.interactor.base.BaseTabInteractor;
import com.hundsun.yr.universal.p2p.ui.activity.base.BaseFragment;
import com.hundsun.yr.universal.p2p.ui.fragment.HomeFragment;
import com.hundsun.yr.universal.p2p.ui.fragment.InvestFragment;
import com.hundsun.yr.universal.p2p.ui.fragment.LoanFragment;
import com.hundsun.yr.universal.p2p.ui.fragment.MineFragment;
import com.hundsun.yr.universal.p2p.ui.fragment.ProductFragment;

import java.util.ArrayList;
import java.util.List;


/**
 * @version V1.0
 * @Title: HomeInteractorImpl.java
 * @Description: 获取home数据
 * @date 2015-10-15 下午4:02:15
 * @author:hejun.shen
 * @email:shenhj15971@hundsun.com
 * @replace author:
 * @replace date:
 */
public class MainInteractorImpl implements BaseTabInteractor {

    /**
     * Tabhost文字数组数组
     */
    private String[] tabLables = null;

    @Override
    public List<BaseFragment> getTabFragments(Context context) {
        // TODO Auto-generated method stub
        List<BaseFragment> fragments = new ArrayList<BaseFragment>();
        String[] tabs = context.getResources().getStringArray(R.array.tab_name);

        for (int i = 0; i < tabs.length; i++) {
            if (TextUtils.equals(tabs[i], context.getString(R.string.tab_home))) {
                fragments.add(i, new HomeFragment());
            } else if (TextUtils.equals(tabs[i], context.getString(R.string.tab_invest))) {
//                if (TextUtils.equals("true", context.getString(R.string.if_transfer))) {
//                    fragments.add(i, new InvestFragment());
//                } else {
                    fragments.add(i, new ProductFragment());
//                }
            } else if (TextUtils.equals(tabs[i], context.getString(R.string.tab_loan))) {
                fragments.add(i, new LoanFragment());
            } else if (TextUtils.equals(tabs[i], context.getString(R.string.tab_mine))) {
                fragments.add(i, new MineFragment());
            }
        }
        return fragments;
    }


    @Override
    public List<View> getTabItem(Context context) {
        // TODO Auto-generated method stub
        TypedArray a = context.getResources().obtainTypedArray(R.array.tab_icon);
        tabLables = context.getResources().getStringArray(R.array.tab_name);
        List<View> tabItem = new ArrayList<View>();
        for (int i = 0; i < tabLables.length; i++) {
            tabItem.add(getItem(context, i, a));
        }
        a.recycle();
        return tabItem;
    }

    /**
     * 获取tabhost每个按钮视图
     *
     * @return
     */
    @SuppressLint("InflateParams")
    public View getItem(Context context, int index, TypedArray a) {

        View view = LayoutInflater.from(context).inflate(R.layout.view_tab_item, null);
        ImageView iv_tab = (ImageView) view.findViewById(R.id.iv_tab);
        iv_tab.setImageResource(a.getResourceId(index, 0));
        TextView tv_tab = (TextView) view.findViewById(R.id.tv_tab);
        tv_tab.setText(tabLables[index]);
        return view;

    }

    @Override
    public String[] getTitles(Context context) {
        // TODO Auto-generated method stub
        return tabLables;
    }

}
